/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.up.ling.irtg.automata


import org.junit.*
import java.util.*
import java.io.*
import de.saar.basic.*
import de.saar.chorus.term.parser.*
import de.up.ling.tree.*
import de.up.ling.irtg.hom.*
import de.up.ling.irtg.algebra.*
import de.up.ling.irtg.signature.*
import com.google.common.collect.Iterators;
import static org.junit.Assert.*
import static de.up.ling.irtg.util.TestingTools.*;

/**
 *
 * @author koller
 */
class DeterminizeTest {
	
    @Test
    public void testDeterminize() {
        TreeAutomaton automaton = pa("q1! -> f(q2, q3)\n q4 -> f(q2, q5)\n q2 -> a\n q3 -> a\n q5 -> a")
        TreeAutomaton det = automaton.determinize();
        
        assert det.accepts(pt("f(a,a)"))       
        
        //        System.out.println(automaton.determinize())
    }
    
    public static final String ELEPHANT = """S! -> r1(NP, VP) [1.0]
NP -> r2 [1.0]
NP -> r3 [1.0]
NP -> r4 [1.0]
NP -> r5(NP, PP) [1.0]
VP -> r6(V, NP) [1.0]
VP -> r7(VP, PP) [1.0]
V -> r8 [1.0]
PP -> r9(P, NP) [1.0]
P -> r10 [1.0]
NP -> r11 [1.0]
V -> r12 [1.0]
NP -> r13 [1.0]
P -> r14 [1.0]
NP -> r15 [1.0]
NP -> r17 [1.0]
NP -> r16 [1.0]""";
    
    @Test
    public void testDeterminize2() {
        TreeAutomaton automaton = pa(ELEPHANT);
        
        TreeAutomaton det = automaton.determinize();
        
        assert det.accepts(pt("r1(r2,r6(r8,r2))"))
        assert det.accepts(pt("r1(r3,r6(r8,r2))"))
        assert det.accepts(pt("r1(r3,r6(r12,r5(r2,r9(r10,r2))))"))
    }
    
    public static final String ELEPHANT_CHART = """'S,0-7'! -> r1('NP,0-1', 'VP,1-7') [1.0]
'NP,0-1' -> r2 [1.0]
'NP,2-4' -> r3 [1.0]
'NP,5-7' -> r4 [1.0]
'NP,2-7' -> r5('NP,2-4', 'PP,4-7') [1.0]
'VP,1-7' -> r6('V,1-2', 'NP,2-7') [1.0]
'VP,1-4' -> r6('V,1-2', 'NP,2-4') [1.0]
'VP,1-7' -> r7('VP,1-4', 'PP,4-7') [1.0]
'V,1-2' -> r8 [1.0]
'PP,4-7' -> r9('P,4-5', 'NP,5-7') [1.0]
'P,4-5' -> r10 [1.0]

""";
    
    @Test
    public void testDeterminize3() {
        TreeAutomaton automaton = pa(ELEPHANT_CHART);
        
        TreeAutomaton det = automaton.determinize();
        
        assertEquals(new HashSet([pt("r1(r2,r6(r8,r5(r3,r9(r10,r4))))"), pt("r1(r2,r7(r6(r8,r3),r9(r10,r4)))")]), det.language());
    }
}

